/*
*   2021-09-23 11:06 检查完毕 定义转向时间 大循环延时设置
*   app_data设置一下 单独检测一下该模块
*   依赖：app_data(nav obd alarm)、bsp_lcd库、蜂鸣器库
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"

#include "app_data.h"
#include "bsp_lcd.h"
#include "bsp_buzz.h"

static const char *TAG = "APP_LCD";
static void app_alarm_handle(void);
static float app_calculate_time(void);

void app_lcd_task(void *arg)
{
    while (1)
    {
        /*处理数据与逻辑*/
        //更新显示数据
        if (app_data.nav.enable && app_data.ble_enable)
        {
            bsp_display_speed(app_data.nav.speed, 1);
            bsp_display_speed_limit(app_data.nav.speed_limit, (app_data.nav.speed_limit > 0) ? 1 : 0);

            if (app_data.alarm[OVER_SPEED].alarm == 0)
            {
                bsp_display_segment(AREA_BRACKETS_LEFT, (app_data.nav.speed_limit > 0) ? 1 : 0);
                bsp_display_segment(AREA_BRACKETS_RIGHT, (app_data.nav.speed_limit > 0) ? 1 : 0);
            }

            bsp_display_segment(AREA_KM, 0);
            bsp_display_segment(AREA_PHONE, app_data.phone);
            bsp_display_segment(AREA_CAMERA, app_data.nav.camera);
            bsp_display_distance((uint16_t)app_data.nav.next_dist, (app_data.nav.next_dist < 10000 && app_data.nav.next_dir < 7) ? 1 : 0);

#ifdef CONFIG_DIR_TIME_CALCULATE
            float next_time = app_calculate_time(app_data); //计算达到下个路口的距离
            if (next_time <= CONFIG_DIR_TIME)
                bsp_display_dir(app_data.nav.next_dir, (app_data.nav.next_dir > 0) ? 1 : 0); //提前18s，开始指示方向
            else
                bsp_display_dir(1, 1); //离路口太远，显示直行
#else
            bsp_display_dir(app_data.nav.next_dir, (app_data.nav.next_dir < 7) ? 1 : 0); //立即显示方向
#endif
        }
        else
        {
            bsp_display_dir(0, 0);
            bsp_display_distance(0, 0);
            bsp_display_speed_limit(0, 0);
            bsp_display_segment(AREA_KM, 0);
            bsp_display_segment(AREA_PHONE, 0);
            bsp_display_segment(AREA_CAMERA, 0);
            bsp_display_segment(AREA_BRACKETS_LEFT, 0);
            bsp_display_segment(AREA_BRACKETS_RIGHT, 0);
        }

        if (app_data.obd.enable)
        {
            bsp_display_speed(app_data.obd.speed, 1);
            bsp_display_water(app_data.obd.water, 1);
            bsp_display_segment(AREA_WATER_C, 1);
            bsp_display_gas(app_data.obd.gas, 1);
            bsp_display_segment(AREA_GAS_PER, 1);
            if (app_data.alarm[HIGH_TEMP].alarm == 0)
            {
                bsp_display_segment(AREA_WATER_SIGN, 1);
            }
            if (app_data.alarm[LOW_GAS].alarm == 0)
            {
                bsp_display_segment(AREA_GAS_ADD, 1);
            }
        }
        else
        {
            bsp_display_water(0, 0);
            bsp_display_segment(AREA_WATER_C, 0);
            bsp_display_gas(0, 0);
            bsp_display_segment(AREA_GAS_PER, 0);
            bsp_display_segment(AREA_WATER_SIGN, 0);
            bsp_display_segment(AREA_GAS_ADD, 0);
        }

        if (app_data.ble_enable || app_data.obd.speed > 0)
        {
            bsp_display_segment(AREA_CAR_BELOW, 1);
        }
        else
        {
            bsp_display_segment(AREA_CAR_BELOW, 0);
        }

        if (app_data.obd.enable || app_data.nav.enable)
        {
            bsp_display_segment(AREA_KMH, 1);
        }
        else
        {
            bsp_display_segment(AREA_KMH, 0);
            bsp_display_speed(0, 0);
        }

        app_alarm_handle(); //报警检测部分
        bsp_display_road(3, 2, (app_data.ble_enable || app_data.obd.speed > 0) ? 1 : 0);
        bsp_lcd_refresh();
        vTaskDelay(20 / portTICK_PERIOD_MS);
    }
}

/** 警报处理函数 **/
static void app_alarm_handle(void)
{
    static uint32_t past_tick = 0;

    /* 判断是否满足条件 警报复位*/
    app_data.alarm[OVER_SPEED].alarm = (app_data.nav.enable && app_data.nav.speed_limit > 0 && ((app_data.obd.enable && app_data.obd.speed > app_data.nav.speed_limit) || (app_data.nav.speed > app_data.nav.speed_limit))) ? 1 : 0;
    app_data.alarm[OVER_SPEED].beep_done = (app_data.alarm[OVER_SPEED].alarm == 0 && app_data.alarm[OVER_SPEED].beep_done == 1) ? 0 : app_data.alarm[OVER_SPEED].beep_done;

    app_data.alarm[HIGH_TEMP].alarm = (app_data.obd.enable && (app_data.obd.water > 80)) ? 1 : 0;
    app_data.alarm[HIGH_TEMP].beep_done = (app_data.alarm[HIGH_TEMP].alarm == 0 && app_data.alarm[HIGH_TEMP].beep_done == 1) ? 0 : app_data.alarm[HIGH_TEMP].beep_done;

    app_data.alarm[LOW_GAS].alarm = (app_data.obd.enable && (app_data.obd.gas < 10)) ? 1 : 0;
    app_data.alarm[LOW_GAS].beep_done = (app_data.alarm[LOW_GAS].alarm == 0 && app_data.alarm[LOW_GAS].beep_done == 1) ? 0 : app_data.alarm[LOW_GAS].beep_done;

    app_data.alarm[TIRED].alarm = 0;
    app_data.alarm[TIRED].beep_done = (app_data.alarm[TIRED].alarm == 0 && app_data.alarm[TIRED].beep_done == 1) ? 0 : app_data.alarm[TIRED].beep_done;
    // alarm[TIRED].alarm = app_tired_judge(app_data);

    //统一蜂鸣
    for (uint8_t i = 0; i < ALARM_NUM; i++)
    {
        if (app_data.alarm[i].alarm == 1 && app_data.alarm[i].beep_done == 0)
        {
            app_data.alarm[i].beep_done = 1;
            bsp_alarm_once();
        }
    }

    //统一闪烁
    static uint8_t flip = 0;
    if (app_tick - past_tick > 500)
    {
        past_tick = app_tick;
        flip = !flip;
        for (uint8_t i = 0; i < ALARM_NUM; i++)
        {
            if (app_data.alarm[i].alarm == 1)
            {
                bsp_display_segment(app_data.alarm[i].area[0], flip);
                if (i == OVER_SPEED)
                {
                    bsp_display_segment(app_data.alarm[i].area[1], flip);
                }
            }
        }
    }
}

/** 判断到达下个路口时间 **/
static float app_calculate_time(void)
{
    if (app_data.obd.enable)
    {
        return (app_data.nav.next_dist / app_data.obd.speed) * 3.6;
    }
    else if (app_data.nav.enable)
    {
        return (app_data.nav.next_dist / app_data.nav.speed) * 3.6;
    }

    return 0xffffffff;
}